/*
 *@Type CmdClient.java
 * @Desc
 * @Author urmsone urmsone@163.com
 * @date 2024/6/13 13:58
 * @version
 */
package client;




import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Scanner;

public class CmdClient implements ClientAPI{
    private Scanner sc = new Scanner(System.in);
    //接收用户命令
    public void acceptUser() throws UnknownHostException {
        System.out.println("服务端已连接");
        System.out.println();
        System.out.println("示例(其中key和value输入自己想要的数据)：\n增加修改命令：set key value\n删除命令：rm key\n查找命令：get key\n输入exit退出");
        String choose;
        while (true){
            choose = sc.nextLine();
            String[] split = choose.split(" ");
            split[0] = split[0].toLowerCase();
            if (split[0].equals("exit")) {
                break;
            }else if(split.length < 2 || (!split[0].equals("set") && !split[0].equals("rm") && !split[0].equals("get"))){
                System.out.println("请输入正确格式");
            } else if (split[0].equals("set") && split.length < 3) {
                System.out.println("请输入正确格式");
            } else{
                performAction(split);
            }
        }
    }

    @Override
    public void performAction(String[] data) {
        switch (data[0]){
            case "set":
                //将data数据除第一、二个之外的数据使用空格连接
                String value = String.join(" ",Arrays.copyOfRange(data,2,data.length));
                socketClient.set(data[1],value);
                break;
            case "rm":
                socketClient.rm(data[1]);
                break;
            case "get":
                socketClient.get(data[1]);
                break;
        }
    }

}
